
package com.kweative.lingua.server.request;

import com.kweative.lingua.server.ProtocolPool;
import com.kweative.lingua.server.protocol.Protocol;
import com.kweative.lingua.server.session.Session;
import com.kweative.network.HttpRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author henrik
 */
public class HttpRequestHandler extends HttpRequest implements RequestHandler {
    Protocol responseProtocol = null;
    private int timeout = 1000;
    @Override
    public void read(InputStream in) throws IOException {
        super.read(in);
        if (getData() != null) {
            try {
                String formatName = (String)getData().getChild("output").getValue();
                responseProtocol = ProtocolPool.instance().getProtocol(formatName + "protocol");
            } catch (Exception ex) {}
        }
    }


    public Protocol getResponseProtocol() {
        return responseProtocol;
    }

    public void setResponseProtocol(Protocol protocol) {
        responseProtocol = protocol;
    }

    public boolean isPersistent() {
        return getTimeout() > 0;
    }

    public int getTimeout() {
        if (headers().containsKey("keep-alive"))
            return Integer.valueOf(headers().get("keep-alive").toString());
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
}
